package com.hmall.cart.listeners;

import cn.hutool.core.util.ObjUtil;
import com.hmall.api.cart.po.OrderList;
import com.hmall.cart.service.ICartService;
import com.hmall.common.constant.MqConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * TODO
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-03-20 18:08
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class CartListener {

	private final ICartService cartService;

	@RabbitListener(
		bindings = @QueueBinding(
			value = @Queue(value = MqConstants.CART_CLEAR_KEY, durable = "true"), exchange = @Exchange(value = MqConstants.TRADE_EXCHANGE_NAME, type = ExchangeTypes.DIRECT), key = MqConstants.ORDER_CREATE_KEY))
	public void handleTradePaySuccessQueue(OrderList orderList) {
		log.info("需要删除的订单列表：{}", orderList);
		if (ObjUtil.isNotEmpty(orderList)) {
			Long userId = orderList.getUserId();
			Collection<Long> itemIds = orderList.getOrderList();
			cartService.removeByItemIds(userId, itemIds);
		}
	}

}
